package com.liuweiheng.springcloud.controller;


import com.liuweiheng.springcloud.service.PaymentHystrixFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer/payment/hystrix")
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixFeignService paymentHystrixFeignService;


    @GetMapping("/ok/{id}")
    public String paymentInfoOk(@PathVariable("id") Integer id) {
        return paymentHystrixFeignService.paymentInfoOk(id);
    }


    @GetMapping("/timeout/{id}")
    // 使用特定兜底策略
    /*@HystrixCommand(
            fallbackMethod="paymentInfoTimeoutFallback",
            commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="5000")}
    )*/
    // 使用全局兜底策略
    //@HystrixCommand
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        return paymentHystrixFeignService.paymentInfoTimeout(id);
    }
    public String paymentInfoTimeoutFallback(@PathVariable("id") Integer id){
        return "远程调用方系统繁忙或自身服务出错，请稍后再试";
    }

    public String paymentGlobalFallbackMethod() {
        return "全局服务降级兜底";
    }

}
